#include <string>
#include <memory.h>
#define ISSPACE(x)	((x)==' '||(x)=='\r'||(x)=='\n'||(x)=='\f'||(x)=='\b'||(x)=='\t')

using namespace std;

inline std::string trim_right(const std::string & source,
		const std::string & t = " ")
{
	std::string str = source;
	return str.erase(str.find_last_not_of(t) + 1);
}

inline std::wstring trim_right(const std::wstring & source,
		const std::wstring & t = L" ")
{
	std::wstring str = source;
	return str.erase(str.find_last_not_of(t) + 1);
}

//   trim_left()   family.
inline std::string trim_left(const std::string & source, const std::string & t =
		"   ")
{
	std::string str = source;
	return str.erase(0, source.find_first_not_of(t));
}

inline std::wstring trim_left(const std::wstring & source,
		const std::wstring & t = L" ")
{
	std::wstring str = source;
	return str.erase(0, source.find_first_not_of(t));
}

//   trim()   family.
inline std::string trim(const std::string & source, const std::string & t = " ")
{
	std::string str = source;
	return trim_left(trim_right(str, t), t);
}

inline std::wstring trim(const std::wstring & source, const std::wstring & t =
		L" ")
{
	std::wstring str = source;
	return trim_left(trim_right(str, t), t);
}

char * trim(char *String)
{
	char *Tail, *Head;
	for (Tail = String + strlen(String) - 1; Tail >= String; Tail--)
	{
		if (!ISSPACE( *Tail ))
		{
			break;
		}
	}
	Tail[1] = 0;
	for (Head = String; Head <= Tail; Head++)
	{
		if (!ISSPACE( *Head ))
		{
			break;
		}
	}
	if (Head != String)
	{
		memcpy(String, Head, (Tail - Head + 2) * sizeof(char));
	}
	return String;
}

